/**
 * Created by zheng on 2018/7/2.
 */
$(function () {
    $('#employee_dg').datagrid({
        url: '/employee/list',

        fit: true,
        fitColumns: true,
        //分页条
        pagination: true,
        // fit:true,
        //工具栏
        toolbar: "#tb",
        //单选
        singleSelect:true,

        columns: [/*[
         {field: 'baseInfo', title: '基本属性', colspan: 2, width: 100, align: "center"},
         {field: 'otherInfo', title: '其他属性', colspan: 3, width: 100, align: "center"}
         ],*/ [
            {field: 'x', checkbox: 'true'},
            {field: 'id', title: '编码', width: 100},
            {field: 'username', title: '员工账号', width: 100},
            {field: 'realname', title: '真实姓名', width: 100},
            {field: 'tel', title: '电话', width: 100},
            {field: 'email', title: '邮箱', width: 100},
            {field: 'hiredate', title: '入职时间', width: 100},
            {
                field: 'dept', title: '部门', width: 100,
                formatter: function (value, row, index) {
                    return value ? value.name : "未分配部门";
                }

                /*, align: 'right'*/
            },
            {
                field: 'admin', title: '管路员', width: 100,
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return "是";
                    } else if (value == 0) {
                        return "否";
                    }
                }
            },
            {
                field: 'state', title: '状态', width: 100,
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return "<font color='green'>在职</font>";

                    } else if (value == 0) {
                        return "<font color='red'>离职</font>";
                    }
                }
            }
        ]],
        //点击行时响应事件
        onClickRow: function (index, row) {

            if (row.state == 1) {
                $("#changstate").linkbutton({
                    text: "离职"
                })
            } else {
                $("#changstate").linkbutton({
                    text: "复职"
                })
            }

        },
        //新增
        border:false,
        cls:"table table-very table-basic"
    })

    //准备输入的对话弹框
    $('#dd').dialog({
        title: 'My Dialog',
        width: 400,
        height: 400,
        closed: true,
        // cache: false,
        modal: true,
        buttons: "#bb"

    })

    //将工具栏所有的函数集合在一个对象中
    var cmdObj;
    cmdObj = {
        //刷新emp页面
        reload: function () {
            $('#employee_dg').datagrid("load")
        },
        //添加
        add: function () {
            //清除以前的数据
            $('#ff').form("clear");
            $('#dd').dialog("open")
            $('#dd').dialog("setTitle", "员工添加")
        },
        //改变emp状态
        changState: function () {
            var row = $('#employee_dg').datagrid("getSelected");
            if (row == null) {
                $.messager.alert("温馨提示", "请选择一行");
                return;
            }
            $.get("/employee/changState", {id: row.id}, function (data) {
                if (data.succeed) {
                    $.messager.alert("温馨提示", "更新状态成功");
                    $('#employee_dg').datagrid("reload")
                } else {
                    $.messager.alert("温馨提示", "删除状态失败");
                }
            })
        },
        edit: function () {

            var row = $('#employee_dg').datagrid("getSelected");
            if (row == null) {
                $.messager.alert("温馨提示", "请选择一行");
                return;
            }
            //console.log(row);
            $('#ff').form("clear");
            $('#dd').dialog("open")
            $('#dd').dialog("setTitle", "员工编辑")
            //处理部门回显
            if (row.dept) {
                row["dept.id"] = row.dept.id;

            }
            //清除以前的数据

            //回显
            $('#ff').form('load', row);

            //角色数据的回显
            $.get("/role/queryRoleIdsByEmpId?empId="+row.id,
                function (data) {
                $("#roleIds").combobox("setValues",data);
            })


        },
        //高级查询
        query: function () {
            var keyword = $("#keyword").textbox("getValue");
            var beginDate = $("#beginDate").textbox("getValue")
            var endDate = $("#endDate").textbox("getValue")
            //发送请求查询最新结果
            $.post("/employee/list",{keyword:keyword, beginDate:beginDate,
                            endDate:endDate})

            $('#employee_dg').datagrid('load', {
                keyword: keyword,
                beginDate: beginDate,
                endDate: endDate
            });

        },
        //保存数据[按钮]
        save: function () {

           /* //获取id
            var id = $("#empId").val();
            console.log(id);*/
            var url = "/employee/saveOrUpdate";
            //提交表单
            $('#ff').form('submit', {
                url: url,
                onSubmit:function (param) {
                  //提交非同名的数据 role[0].id
                    var roleIds = $("#roleIds").combobox("getValues");
                    for(var i=0;i<=roleIds.length-1;i++){
                        param["roles["+i+"].id"] = roleIds[i];
                    }
                },
                success: function (data) {
                    //把字符串转换为json
                    var data = $.parseJSON(data);
                    if (!data.succeed) {
                        $.messager.alert('温馨提示', data.errorMsg);
                    } else {
                        $.messager.alert('温馨提示', "保存成功");
                        //关闭对话框
                        $('#dd').dialog("close");
                        //刷新datagrid
                        $('#employee_dg').datagrid("reload")
                    }
                }
            });
        },
        //关闭按钮
        cancel: function () {
            $('#dd').dialog("close")
        }


    };

    //调用函数集合对象
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })
})




